package com.zhou.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.entity.Input;
import com.zhou.entity.WareHouse;
import com.zhou.mapper.InputMapper;
import com.zhou.mapper.WareHouseMapper;
import com.zhou.service.InputService;
import com.zhou.utis.booleanUtils.BooleanEaun;
import com.zhou.vo.inputVo.InputVo;
import com.zhou.vo.inputVo.UpdateInputVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class InputServiceImpl implements InputService {
    @Autowired
    private InputMapper inputMapper;
    @Autowired
    private WareHouseMapper wareHouseMapper;

    @Override
    public Boolean save(InputVo vo) {
        if (StringUtils.isEmpty(vo)) {
            throw new RuntimeException("参数不能为空，请重新输入");
        }

        //需要判断库存里面是否有该物资，如果有就直接修改数量，没有就新增
        LambdaQueryWrapper<WareHouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WareHouse::getMaterialName, vo.getMaterialName());
        wrapper.eq(WareHouse::getDelFlag, BooleanEaun.FLASE.getResultValue());
        WareHouse wareHouse = wareHouseMapper.selectOne(wrapper);
        WareHouse wh = new WareHouse();
        if (!StringUtils.isEmpty(wareHouse)) {
            wh.setExpireTime(wareHouse.getExpireTime());
        }

        Integer materialNumber = wareHouse.getMaterialNumber();
        wh.setMaterialNumber(materialNumber + vo.getMaterialNumber());
        wh.setMaterialType(vo.getMaterialType());
        wh.setMaterialName(vo.getMaterialName());
        String uuid = IdUtil.fastUUID();
        if (StringUtils.isEmpty(wareHouse)) {
            //如果为空就新增
            wh.setWareHouseId(uuid);
            int insert = wareHouseMapper.insert(wh);
            if (insert == 0) {
                throw new RuntimeException("新增失败");
            }
        } else {
            //如果不是空就修改
            int i = wareHouseMapper.updateById(wh);
            if (i == 0) {
                throw new RuntimeException("修改数据失败，请重试");
            }
        }
        Input input = new Input();
        //上面总库操作完，还需要添加入库表数据
        input.setInputId(UUID.fastUUID().toString());
        input.setExpireTime(vo.getExpireTime());
        input.setMaterialName(vo.getMaterialName());
        input.setSource(vo.getSource());
        input.setMaterialType(vo.getMaterialType());
        if (StringUtils.isEmpty(wareHouse)) {
            input.setWareHouseId(uuid);
        } else {
            input.setWareHouseId(wareHouse.getWareHouseId());
        }
        input.setMaterialNumber(vo.getMaterialNumber());
        int insert = inputMapper.insert(input);
        if (insert == 0) {
            throw new RuntimeException("入库表新增失败");
        }
        return true;
    }

    /**
     * 修改入库操作
     *
     * @param vo
     * @return
     */
    @Override
    public Boolean update(UpdateInputVo vo) {
        if (StringUtils.isEmpty(vo) || StringUtils.isEmpty(vo.getInputId())) {
            throw new RuntimeException("入参不能为空");
        }
        Input input = inputMapper.selectById(vo.getInputId());
        if (StringUtils.isEmpty(input)) {
            throw new RuntimeException("没有查到该条记录，请重试");
        }
        //需要判断数量是否有更改，如果有就需要调整仓库表里的数据
        if (!input.getMaterialNumber().equals(vo.getMaterialNumber())) {
            WareHouse wareHouse = new WareHouse();
            //查出旧数据
            WareHouse wareHouse1 = wareHouseMapper.selectById(input.getWareHouseId());
            BeanUtils.copyProperties(wareHouse1, wareHouse);
            int num = 0;
            if (input.getMaterialNumber() < vo.getMaterialNumber()) {
                //旧数据比新数据小
                num = vo.getMaterialNumber() - input.getMaterialNumber();
                wareHouse.setMaterialNumber(wareHouse1.getMaterialNumber() + num);
            } else {
                //旧数据比新数据大
                num = input.getMaterialNumber() - vo.getMaterialNumber();
                wareHouse.setMaterialNumber(wareHouse1.getMaterialNumber() - num);
            }
            int two = wareHouseMapper.updateById(wareHouse);
            if (two == 0) {
                throw new RuntimeException("修改失败，请重试");
            }
        }
        BeanUtils.copyProperties(vo, input);
        int i = inputMapper.updateById(input);
        if (i == 0) {
            throw new RuntimeException("入库操作修改失败");
        }
        return true;

    }

    /**
     * 删除方法 软删除
     *
     * @param id
     * @return
     */
    @Override
    public Boolean delete(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("参数不能为空");
        }
        Input input = inputMapper.selectById(id);
        if (StringUtils.isEmpty(input)) {
            throw new RuntimeException("没有查到数据，请重试");
        }
        input.setDelFlag(1);
        int i = inputMapper.updateById(input);
        if (i == 0) {
            throw new RuntimeException("该物资已经被删除了，请重试");
        }
        LambdaQueryWrapper<WareHouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WareHouse::getWareHouseId, input.getWareHouseId());
        wrapper.eq(WareHouse::getDelFlag, BooleanEaun.FLASE.getResultValue());
        WareHouse wareHouse = wareHouseMapper.selectOne(wrapper);
        if (StringUtils.isEmpty(wareHouse)) {
            throw new RuntimeException("仓库数据没有了，请联系管理员添加");
        }
        wareHouse.setMaterialNumber(wareHouse.getMaterialNumber() - input.getMaterialNumber());
        int i1 = wareHouseMapper.updateById(wareHouse);
        if (i1 == 0) {
            throw new RuntimeException("修改仓库失败，请重试");
        }
        return true;
    }

    /**
     * 根据id获取用户信息
     *
     * @param id
     * @return
     */
    @Override
    public Input getById(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("参数不能为空，请重试");
        }
        Input input = inputMapper.selectById(id);
        if (StringUtils.isEmpty(input)) {
            throw new RuntimeException("没有查询到数据吗，请重试");
        }
        return input;
    }

    /**
     * 分页查询
     *
     * @param page     1 当前页
     * @param pageSize 10 每页展示数据的条数
     * @return
     */
    @Override
    public List<Input> getAll(Integer page, Integer pageSize) {
        Page<Input> pg = new Page<>(page, pageSize);
        LambdaQueryWrapper<Input> wrapper = new LambdaQueryWrapper<>();
        List<Input> records = inputMapper.selectPage(pg, wrapper).getRecords();
        if (CollectionUtils.isEmpty(records)) {
            throw new RuntimeException("查询失败，没有数据，请重试");
        }
        return records;
    }
}
